do
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local bb,i = {},0 for i=1,#b do bb[b:sub(i,i)]=i-1 end
function encdate(y,m,d) return (tonumber(d)-1)+(tonumber(m)-1)*31+(tonumber(y)-8)*372 end
function decdate(sum) return math.floor(sum/372)+8, math.floor(sum %372/31)+1, sum%31+1 end
function enc(sums) local r = ""; for _, v in pairs(sums) do r = r..b:sub(math.floor(v/64)+1,math.floor(v/64)+1)..b:sub(v%64+1,v%64+1) end return r end
function dec(str) 
	if(#str==2) then return bb[str:sub(1,1)]*64+bb[str:sub(2,2)] end
	local sums = {}
	for i=1,#str,2 do
		local sum=bb[str:sub(i,i)]*64+bb[str:sub(i+1,i+1)]
		table.insert(sums,sum) 
	end
	return sums 
end

function strsplit(str, sep)
	local r = {}
	while(str) do
		if str:find(sep) then
			table.insert(r, str:sub(1,str:find(sep)-1))
			str = str:sub(str:find(sep)+#sep)
		else
			table.insert(r, str)
			str = nil
		end
	end
	return r
end

local archs = {1176,1177,1178,1017,2097,556,557,558,705,621,6,8,10,11,12,13,545,964,891,889,890,892,546,2716,1180,973,974,975,976,503,504,505,506,507,508,31,238,515,397,1157,218,166,1308,3776,1717,2085,2086,2087,2088,2089,4476,4477,4478,3839,3841,3842,3844,3876,3778,4516,2154,4521,4297,490,494,500,1817,2046,2044,2045,493,495,1871,1867,1862,1868,2150,1860,733,116,734,735,131,134,135,522,523,1009,1008,1007,947,}
local MW_LEN = #archs*2 + 5
function MW_IsSame(s1, s2) --s1是游戏准确值,可能是几天前的,s2是列表值,可能是英雄榜有延迟
	s1=s1:sub(s1:find("^",1,true)+1)
	s2=s2:sub(s2:find("^",1,true)+1)
	if(s1:sub(3,5)~=s2:sub(3,5)) then return false end
	for i=6, MW_LEN, 2 do
		local t1 = s1:sub(i,i+1)
		local t2 = s2:sub(i,i+1)
		if(t1~=t2) then
			if t1=="AA" then 
				local d1 = dec(s1:sub(1,2))
				if dec(t2)<d1 then return false end
			elseif t2=="AA" then
				local d2 = dec(s2:sub(1,2))
				if dec(t1)<d2-1 then return false end
			else
				return false
			end
		end
	end
	return true
end

function MW_GetNameFromDNA(s)
	return s:sub(1,s:find("^",1,true)-1)
end
if(MW_LIST) then
SUM={}
MW_LIST_NEW = {}
for i=1, #(MW_LIST) do
	local sum=0
	local sss = select(3,string.find(MW_LIST[i][1],"([^%^]*)$"))
	for j=6,#sss,2 do
		if "AA"~=sss:sub(j,j+1) then sum=sum+1 end
	end
	print("total:"..sum.." - "..MW_GetNameFromDNA(MW_LIST[i][1]))
	local issame = false
	for j=1,#(MW_LIST_NEW) do
		if MW_IsSame(MW_LIST[i][1], MW_LIST_NEW[j][1]) or MW_IsSame(MW_LIST_NEW[j][1], MW_LIST[i][1]) then
			issame = true
			print("IsSame: "..MW_GetNameFromDNA(MW_LIST[i][1]).." -> "..MW_GetNameFromDNA(MW_LIST_NEW[j][1]))
			local d1 = dec(MW_LIST[i][1]:sub(MW_LIST[i][1]:find("^",9,true)+1):sub(1,2))
			local d2 = dec(MW_LIST_NEW[j][1]:sub(MW_LIST_NEW[j][1]:find("^",9,true)+1):sub(1,2))
			if d1>d2 then
				MW_LIST_NEW[j][1] = MW_LIST[i][1]
			end
			if MW_LIST[i][2] and #MW_LIST[i][2] > #MW_LIST_NEW[j][2] then
				MW_LIST_NEW[j][2] = MW_LIST[i][2]
				MW_LIST_NEW[j][3] = MW_LIST[i][3]
				MW_LIST_NEW[j][4] = MW_LIST[i][4]
			end

			local name = MW_GetNameFromDNA(MW_LIST[i][1])
			
			local samename = false
			for _, oldname in pairs(MW_LIST_NEW[j][5]) do
				if name==oldname:sub(oldname:find("%)")+1) then samename=true break end
			end
			if not samename then
				local y, m, d = decdate(d1)
				table.insert(MW_LIST_NEW[j][5], "(20"..y.."-"..m.."-"..d..")"..name)
			end
		end
	end

	if not issame then
		local y, m, d = decdate(dec(MW_LIST[i][1]:sub(MW_LIST[i][1]:find("^",9,true)+1):sub(1,2)))
		if type(MW_LIST[i][5])=="string" then
			MW_LIST[i][5] = strsplit(MW_LIST[i][5], ",")
		else
			MW_LIST[i][5] = {  "(20"..y.."-"..m.."-"..d..")"..MW_GetNameFromDNA(MW_LIST[i][1]) }
		end
		table.insert(MW_LIST_NEW, MW_LIST[i])
		classId = MW_LIST[i][1]:sub(MW_LIST[i][1]:find("^",9,true)+3, MW_LIST[i][1]:find("^",9,true)+3)
		SUM[classId] = (SUM[classId] or 0) + 1
	end
end
ARMORY_CLASS = {
	["1"] = "WARRIOR",
	["2"] = "PALADIN",
	["3"] = "HUNTER",
	["4"] = "ROGUE",
	["5"] = "PRIEST",
	["6"] = "DKNIGHT",
	["7"] = "SHAMAN",
	["8"] = "MAGE",
	["9"] = "WARLOCK",
	["B"] = "DRUID",
}
print("")
ssss="" for _, v in pairs({"1","2","3","4","5","6","7","8","9","B"}) do ssss=ssss..ARMORY_CLASS[v].."\t\t= "..SUM[v].."\n" end
print(ssss)
print("total: "..#MW_LIST_NEW)
local io = require("io")
file = io.open("WantedListNew.lua", "w")
file:write("MW_LIST = {\n\n")
for _, v in pairs(MW_LIST_NEW) do
	file:write("{\n")
	file:write("\t\""..v[1].."\",\n")
	if v[2] then file:write("\t\""..v[2].."\",\n") end
	if v[3] then file:write("\t\""..v[3].."\",\n") end
	if v[4] then file:write("\t\""..v[4].."\",\n") end
	if v[5] and #v[5] > 1 then
		file:write("\t\""..table.concat(v[5],",").."\",\n")
	end
	file:write("},\n")
end
file:write("-- total: "..#MW_LIST_NEW.."\n")file:write("\n}")file:close()
os.remove("WantedList.lua."..os.date("%Y%m%d%H%M"))
--if not os.rename("WantedList.lua", "WantedList.lua."..os.date("%Y%m%d%H%M")) then os.remove("WantedList.lua") end
os.rename("WantedListNew.lua", "WantedList.lua."..os.date("%Y%m%d%H%M")) end end --http://tw.wowarmory.com/character-feed.atom?r=戰歌&cn=雯姿,不不魚,風火雲影,回到起點,秀發&filters=BOSSKILL,LOOT&locale=zh_TW